package com.example.elm_server_api.controller;

import com.example.elm_server_api.domain.AdminUser;
import com.example.elm_server_api.domain.Shop;
import com.example.elm_server_api.domain.ShopInfo;
import com.example.elm_server_api.domain.UserMsg;
import com.example.elm_server_api.service.IAdminService;
import com.example.elm_server_api.service.IShopInfoService;
import com.example.elm_server_api.service.IShopService;
import com.example.elm_server_api.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/meituan")
@CrossOrigin
public class AdminController {

    @Autowired
    IAdminService adminService;

    @Autowired
    IShopService shopService;

    @Autowired
    IUserService userService;

    @Autowired
    IShopInfoService shopInfoService;

    //管理员登录
    @PostMapping("/login/admin")
    public AdminUser login(AdminUser adminUser) {
        return this.adminService.selectByLogin(adminUser);
    }

    //查找所有商家
    @GetMapping("/findAll/shop")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public List<Shop> findAllShop() {
        return this.shopService.selectAllShop();
    }

    //删除商家
    @PostMapping("/delete/shop")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public int deleteShop(Long id) {
        return this.shopService.deleteByPrimaryKey(id);
    }

    //修改商家信息
    @PostMapping("/update/shop")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public int updateShop(@RequestBody Shop shop) {
        return this.shopService.updateByShopIdSelective(shop);
    }

    //查找所有用户
    @GetMapping("/findAll/user")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public List<UserMsg> findAllUser() {
        return this.userService.selectAll();
    }

    //删除用户
    @PostMapping("/delete/user")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public int deleteUser(Long id) {
        return this.userService.deleteByPrimaryKey(id);
    }

    //修改用户信息
    @PostMapping("/update/user")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public int updateUser(@RequestBody UserMsg userMsg) {
        return this.userService.updateByPrimaryKeySelective(userMsg);
    }

    //按商铺名和菜品名进行模糊查询
    @PostMapping("/shopInfo/user")
    public List<ShopInfo> selectName(String name) {
        return this.shopInfoService.selectByName(name);
    }
}
